package com.nuoniu.sibanyun.service.expenses.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.constant.OrderConstant;
import com.nuoniu.sibanyun.common.lang.DateUtils;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.expenses.ExpensesPaymentOrder;
import com.nuoniu.sibanyun.entity.expenses.dto.ExpensesPaymentOrderSearchDto;
import com.nuoniu.sibanyun.mapper.expenses.ExpensesPaymentOrderMapper;
import com.nuoniu.sibanyun.service.expenses.IExpensesAccountService;
import com.nuoniu.sibanyun.service.expenses.IExpensesPaymentOrderService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 * 费用中心-支付订单 服务实现类
 * </p>
 *
 * @author 小啊刚
 * @since 2022-05-06
 */
@Service
public class ExpensesPaymentOrderServiceImpl extends ServiceImpl<ExpensesPaymentOrderMapper, ExpensesPaymentOrder> implements IExpensesPaymentOrderService {

    @Autowired
    private IExpensesAccountService expensesAccountService;

    @Override
    public Page<ExpensesPaymentOrder> pageList(ExpensesPaymentOrderSearchDto paymentOrderSearchDto) {
        Integer businessType = paymentOrderSearchDto.getBusinessType();
        String orderNo = paymentOrderSearchDto.getOrderNo();
        String paymentProof = paymentOrderSearchDto.getPaymentProof();
        Integer paymentType = paymentOrderSearchDto.getPaymentType();
        Integer status = paymentOrderSearchDto.getStatus();
        UserInfo userInfo = UserThreadLocal.get();
        Integer companyId = userInfo.getCompanyId();
        QueryWrapper<ExpensesPaymentOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",companyId);
        if(null != businessType){
            queryWrapper.eq("business_type",businessType);
        }
        if(StringUtils.isNotBlank(orderNo)){
            queryWrapper.like("order_no",orderNo);
        }
        if(StringUtils.isNotBlank(paymentProof)){
            queryWrapper.like("payment_proof",paymentProof);
        }
        if(null != paymentType){
            queryWrapper.eq("payment_type",paymentType);
        }
        if(null != status){
            queryWrapper.eq("status",status);
        }
        Page page = page(paymentOrderSearchDto.getPage(), queryWrapper);
        return page;
    }

    @Override
    public ExpensesPaymentOrder createPayOrder(BigDecimal amount) {
        LocalDateTime now = LocalDateTime.now();
        UserInfo userInfo = UserThreadLocal.get();
        ExpensesPaymentOrder paymentOrder = new ExpensesPaymentOrder();
        paymentOrder.setPaymentAmount(amount);
        paymentOrder.setCompanyId(userInfo.getCompanyId());
        paymentOrder.setBusinessType(1);
        Integer serialNum = this.getSerialNum();
        String yyyyMMddHHmmss = DateUtils.formatDate(new Date(), "yyyyMMddHHmmss");
        String orderNo = OrderConstant.PAYMENT_PRE + yyyyMMddHHmmss + "_"+ userInfo.getCompanyId() + "_" + serialNum;
        paymentOrder.setOrderNo(orderNo);
        paymentOrder.setSerialNum(serialNum);
        paymentOrder.setCreateTime(now);
        paymentOrder.setStatus(0);
        this.save(paymentOrder);
        return paymentOrder;
    }

    @Override
    public Integer getSerialNum() {
        UserInfo userInfo = UserThreadLocal.get();
        Integer serialNum = baseMapper.getSerialNum(userInfo.getCompanyId());
        if(null == serialNum)serialNum = 0;
        serialNum ++;
        return serialNum;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void successPayment(ExpensesPaymentOrder expensesPaymentOrder) {
        this.updateById(expensesPaymentOrder);
        expensesAccountService.changeFromPaymentOrder(expensesPaymentOrder);
    }

    @Override
    public ExpensesPaymentOrder getByOrderNo(String orderNo) {
        QueryWrapper<ExpensesPaymentOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo);
        queryWrapper.last("LIMIT 1");
        return getOne(queryWrapper,false);
    }


}
